import { MATERIAL_PREVIEW_DEFAULT_PROPS } from '@app-base/common/utils/materialPreview';
import {
  ABSSvgComponent,
  Aisi1045SvgComponent,
  Aisi304SvgComponent,
  AluminumAlloy2024SvgComponent,
  AluminumAlloy7075SvgComponent,
  AluminumSvgComponent,
  BrassSvgComponent,
  CopperSvgComponent,
  GlassSvgComponent,
  GoldSvgComponent,
  IronSvgComponent,
  PineSvgComponent,
  PowercadDefaultSvgComponent,
  RubberSvgComponent,
  SilverSvgComponent,
  TitaniumAlloyTi5Al25SnSvgComponent,
  FavoriteSvgComponent,
  MaterialLibraryLockedSvgComponent,
  NewMaterialCategorySvgComponent,
  CustomMaterialSvgComponent,
  MaterialLibrarySvgComponent,
} from '../../icons/index';

interface PropertyMeta {
  label: string;
  icon: React.FC<React.SVGProps<SVGSVGElement>>;
  renderProps?: {
    baseColor: string;
    metalness: number;
    roughness: number;
    name?: string; // 用于玻璃材质的特殊处理
    textureFiles?: { key: string; url: string }[];
  };
}
// materialTreeData 二级 key 到 label 和图标 ReactComponent 的映射
export const materialKeyMap: Record<string, PropertyMeta> = {
  // 金属
  'metal-aisi_304': {
    label: '不锈钢 AISI 304',
    icon: Aisi304SvgComponent,
    renderProps: {
      baseColor: '#c0c0c0',
      metalness: 1,
      roughness: 0.4,
    },
  },
  'metal-aisi_1045': {
    label: '钢 AISI 1045',
    icon: Aisi1045SvgComponent,
    renderProps: {
      baseColor: '#a0a0a0',
      metalness: 1,
      roughness: 0.55,
    },
  },
  'metal-brass': {
    label: '黄铜',
    icon: BrassSvgComponent,
    renderProps: {
      baseColor: '#e1c16e',
      metalness: 1,
      roughness: 0.4,
    },
  },
  'metal-gold': {
    label: '金',
    icon: GoldSvgComponent,
    renderProps: {
      baseColor: '#ffd700',
      metalness: 1,
      roughness: 0.2,
    },
  },
  'metal-aluminum': {
    label: '铝',
    icon: AluminumSvgComponent,
    renderProps: {
      baseColor: '#cccccc',
      metalness: 1,
      roughness: 0.4,
    },
  },
  'metal-aluminum_2024': {
    label: '铝合金 2024',
    icon: AluminumAlloy2024SvgComponent,
    renderProps: {
      baseColor: '#bbbbbb',
      metalness: 1,
      roughness: 0.45,
    },
  },
  'metal-aluminum_7075': {
    label: '铝合金 7075',
    icon: AluminumAlloy7075SvgComponent,
    renderProps: {
      baseColor: '#bbbbbb',
      metalness: 1,
      roughness: 0.45,
    },
  },
  'metal-ti_5ai_25sn': {
    label: '钛合金 Ti-5AI-2.5Sn',
    icon: TitaniumAlloyTi5Al25SnSvgComponent,
    renderProps: {
      baseColor: '#888888',
      metalness: 1,
      roughness: 0.55,
    },
  },
  'metal-iron': {
    label: '铁',
    icon: IronSvgComponent,
    renderProps: {
      baseColor: '#909090',
      metalness: 1,
      roughness: 0.6,
    },
  },
  'metal-copper': {
    label: '铜',
    icon: CopperSvgComponent,
    renderProps: {
      baseColor: '#b87333',
      metalness: 1,
      roughness: 0.4,
    },
  },
  'metal-silver': {
    label: '银',
    icon: SilverSvgComponent,
    renderProps: {
      baseColor: '#dcdcdc',
      metalness: 1,
      roughness: 0.2,
    },
  },

  // 非金属
  'nonmetal-abs': {
    label: 'ABS塑料',
    icon: ABSSvgComponent,
    renderProps: {
      baseColor: '#222222',
      metalness: 0,
      roughness: 0.5,
    },
  },
  'nonmetal-glass': {
    label: '玻璃',
    icon: GlassSvgComponent,
    renderProps: {
      baseColor: '#1EE76C',
      name: 'glass',
      metalness: 0,
      roughness: 0,
    },
  },
  'nonmetal-rubber': {
    label: '橡胶',
    icon: RubberSvgComponent,
    renderProps: {
      baseColor: '#111111',
      metalness: 0,
      roughness: 0.8,
    },
  },
  'nonmetal-pine_wood': {
    label: '松木',
    icon: PineSvgComponent,
    renderProps: {
      baseColor: '#e6c48a',
      metalness: 0,
      roughness: 0.6,
      textureFiles: [
        { key: 'map', url: 'textures/woods/pine/Wood005_1K-JPG_Color.jpg' },
        {
          key: 'normalMap',
          url: 'textures/woods/pine/Wood005_1K-JPG_NormalGL.jpg',
        },
        {
          key: 'roughnessMap',
          url: 'textures/woods/pine/Wood005_1K-JPG_Roughness.jpg',
        },
      ],
    },
  },

  // 自定义样式 & 特殊项（不加 renderProps）
  'custom-powercad-styles': {
    label: 'PowerCAD style',
    icon: PowercadDefaultSvgComponent,
    renderProps: { ...MATERIAL_PREVIEW_DEFAULT_PROPS },
  },
  collection: {
    label: '收藏夹',
    icon: FavoriteSvgComponent,
  },
  'standard-material-library': {
    label: '标准材料库',
    icon: MaterialLibraryLockedSvgComponent,
  },
  'material-category': {
    label: '二级文件夹',
    icon: NewMaterialCategorySvgComponent,
  },
  'custom-material-library': {
    label: '自定义材料库',
    icon: MaterialLibrarySvgComponent,
  },
  'custom-material': {
    label: '自定义材料',
    icon: CustomMaterialSvgComponent,
  },
};

const DEFAULT_MATERIAL: PropertyMeta = {
  label: '未知材料',
  icon: CustomMaterialSvgComponent,
  renderProps: { ...MATERIAL_PREVIEW_DEFAULT_PROPS },
};

// 获取材料配置并且做默认配置以防止 key 不存在报错
export function getMaterialConfig(key: string) {
  if (Object.prototype.hasOwnProperty.call(materialKeyMap, key)) {
    return materialKeyMap[key];
  }
  return DEFAULT_MATERIAL;
}
